﻿using EnvDTE80;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EBFHelper.Nodes;
using System.Xml;
using System.Diagnostics;

namespace EBFHelper
{
    public partial class EBFHelperForm : Form
    {
        private DTE2 _applicationObject;

        public DTE2 applicationObject
        {
            get { return _applicationObject; }
            set { this._applicationObject = value; }
        }

        public EBFHelperForm()
        {
            InitializeComponent();
        }

        private void checkProjectItemNode(EnvDTE.ProjectItems projectItems, List<EnvDTE.ProjectItem> pItems)
        {
            foreach (EnvDTE.ProjectItem pi in projectItems)
            {
                //判断是否为三个重要节点
                if (pi.Name.Equals("Actions") || pi.Name.Equals("Operations") || pi.Name.Equals("OpSteps"))
                {
                    pItems.Add(pi);
                }
                if (pItems.Count>=2)
                {
                    break;//起码找到了ACTIONS 以及Operations节点
                }
                //没有子节点
                if (pi.ProjectItems != null)
                {
                    checkProjectItemNode(pi.ProjectItems, pItems);
                }
            }
        }

        private void findNodeList(EnvDTE.ProjectItem parentItem, List<iNode> iNodes, int style)
        {
            foreach (EnvDTE.ProjectItem pi in parentItem.ProjectItems)
            {
                if (0 == style)
                {
                    ActionXmlNode actionXmlNode = new ActionXmlNode();
                    actionXmlNode.projectItem = pi;
                    actionXmlNode.parentItem = parentItem;
                    iNodes.Add(actionXmlNode);
                }
                else if (1 == style)
                {
                    OperationXmlNode operationXmlNode = new OperationXmlNode();
                    operationXmlNode.projectItem = pi;
                    operationXmlNode.parentItem = parentItem;
                    iNodes.Add(operationXmlNode);
                }
                if (pi.ProjectItems != null)
                {
                    findNodeList(pi, iNodes, style);
                }
            }

        }

        private void findActionsAndOperationsAndOpstepsList(List<EnvDTE.ProjectItem> pItems, List<iNode> actionNodes, List<iNode> operationNodes)
        {
            foreach (EnvDTE.ProjectItem pi in pItems)
            {
                //遍历ACTIONS文件夹中的每一个元素
                if (pi.Name.Equals("Actions"))
                {
                    findNodeList(pi, actionNodes, 0);
                }
                //遍历operations文件夹中的每一个元素
                else if (pi.Name.Equals("Operations"))
                {
                    findNodeList(pi, operationNodes, 1);
                }
            }
        }

        private List<iNode> findXmlNodeList(List<iNode> iNodes)
        {
            List<iNode> xmlNodeList = new List<iNode>();
            foreach (iNode inode in iNodes)
            {
                if (!inode.projectItem.Name.EndsWith(".xml"))
                {
                    continue;
                }
                xmlNodeList.Add(inode);
            }
            return xmlNodeList;
        }

        private void addOperationXmlNodeToActionXmlNode(List<iNode> actionXmlList, List<iNode> operationXmlList)
        {
            foreach (iNode actionNode in actionXmlList)
            {
                //ActionXmlNode actionXmlNode =(ActionXmlNode) inode;
                ActionXmlNode actionXmlNode;
                if (null == (actionXmlNode = actionNode as ActionXmlNode))
                {
                    continue;
                }
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(actionXmlNode.projectItem.FileNames[1]);
                XmlNode ebOperation = xmlDoc.SelectSingleNode("//Action_Config//OperationAction");
                if (null== ebOperation)
                {
                    continue;
                }
                XmlElement xn = (XmlElement)ebOperation;
                string eo = xn.GetAttribute("operationName");
                if (string.IsNullOrEmpty(eo))
                {
                    continue;
                }
                foreach (iNode operationNode in operationXmlList)
                {
                    if (operationNode.projectItem.Name.Contains(eo))
                    {
                        actionXmlNode.operationNode = operationNode;
                    }
                }


            }
        }

        private int checkconfigourationInfo()
        {
            try
            {
                //获取解决方案的所有项目
                System.Array theProjects = (System.Array)_applicationObject.ActiveSolutionProjects;

                if (theProjects.Length > 0)
                {
                    //遍历解决方案中的每一个项目
                    foreach (EnvDTE.Project theProject in theProjects)
                    {
                        if (!theProject.Name.Contains("WebSite"))
                        {
                            continue;
                        }
                        List<EnvDTE.ProjectItem> configourations = new List<EnvDTE.ProjectItem>();
                        //遍历项目中的每一个元素
                        checkProjectItemNode(theProject.ProjectItems, configourations);
                        //获取到actions operations opsteps节点
                        if (configourations.Count > 0)
                        {
                            List<iNode> actionNodes = new List<iNode>();
                            List<iNode> operationNodes = new List<iNode>();

                            findActionsAndOperationsAndOpstepsList(configourations, actionNodes, operationNodes);

                            List<iNode> actionXmlList = findXmlNodeList(actionNodes);
                            List<iNode> operationXmlList = findXmlNodeList(operationNodes);

                            addOperationXmlNodeToActionXmlNode(actionXmlList, operationXmlList);
                            // listBox1.Items.Clear();
                            for (int i = 0; i < actionXmlList.Count; i++)
                            {
                                listBox1.Items.Insert(i, actionXmlList[i].projectItem);

                            }
                        }
                    }
                }

                return 0;
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message.ToString());
                return 1;
            }
        }

        private void CheckConfigourationInfoBtn_Click(object sender, EventArgs e)
        {
            checkconfigourationInfo();
        }
    }
}
